// Program.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NetworkFlowMonitor.Scripts.Data;
using NetworkFlowMonitor.Scripts.Service;

var builder = WebApplication.CreateBuilder(args);

// 添加服务到容器
builder.Services.AddRazorPages();
builder.Services.AddSignalR();
builder.Services.AddDbContext<ApplicationDbContext>();
builder.Services.AddHostedService<TrafficMonitorService>();
builder.WebHost.UseUrls("http://0.0.0.0:5000");


 
var app = builder.Build();

// 配置HTTP请求管道
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

// app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();

// 配置端点
app.MapRazorPages();
app.MapHub<TrafficHub>("/trafficHub");
app.MapControllers();

// 确保数据库已创建
using (var scope = app.Services.CreateScope())
{
    var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
    context.Database.EnsureCreated();
}

app.Run();